<template>
  <el-dialog class="assign-role" title="分配角色" :visible="showRoleDialog" @close="close">
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        <el-button size="small" @click="close">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>
<script>
import { getRoleList } from '@/api/setting'
import { getUserDeatilById } from '@/api/user'
import { assignRoles } from '@/api/employees'

export default {
  name: 'AssignRole',
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    userId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      checkList: [],
      list: []
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    async getRoleList() {
      const { rows } = await getRoleList({ page: 1, size: 20 })
      this.list = rows
    },
    async getRoleById(id) {
      const { roleIds } = await getUserDeatilById(id)
      this.checkList = roleIds
    },
    close() {
      this.checkList = []
      this.$emit('update:showRoleDialog', false)
    },
    async btnOk() {
      await assignRoles({ id: this.userId, roleIds: this.checkList })
      this.$message.success('角色分配成功！')
      this.$emit('update:showRoleDialog', false)
    }
  }
}
</script>

<style scoped lang="scss">
.assign-role {
  ::v-deep .el-dialog__header {
    background-color: rgb(29, 63, 255);
    .el-dialog__title,
    .el-dialog__close {
      color: #fff;
    }
  }
}
</style>
